package per.lee.spingboothelper.db.export;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import per.lee.spingboothelper.common.export.AbsExportSpanModel;
import per.lee.spingboothelper.db.model.SlowSqlModel;

/**
 * @description: slow sql export
 * @author: lixiaoying
 * @create: 2019-02-28 09:11
 **/

public class SlowSqlExport extends AbsExportSpanModel {

	private List<SlowSqlModel> voList;
	private String fileName;

	public SlowSqlExport(List<SlowSqlModel> voList, String fileName) {
		this.voList = voList;
		this.fileName = fileName;
	}

	public static Boolean export(HttpServletRequest request, HttpServletResponse response, String fileName,
			List<SlowSqlModel> voList) {
		AbsExportSpanModel model = new SlowSqlExport(voList, fileName);
		return model.print(request, response, fileName);
	}

	public List<String> getTitle() {
		ArrayList<String> titleList = new ArrayList<>();
		titleList.add("sql");
		titleList.add("耗时");
		titleList.add("selectType");
		titleList.add("type");
		titleList.add("possibleKeys");
		titleList.add("key");
		titleList.add("keyLen");
		titleList.add("ref");
		titleList.add("rows");
		titleList.add("extra");
		return titleList;
	}

	@Override
	public List<List<Object>> getRows() {
		List<List<Object>> rows = new ArrayList(voList.size());
		voList.stream().forEach(vo -> {
			List<Object> row = new ArrayList();
			row.add(vo.getSql());
			row.add(vo.getExecuteTime());
			row.add(vo.getSelectType());
			row.add(vo.getType());
			row.add(vo.getPossibleKeys());
			row.add(vo.getKey());
			row.add(vo.getKeyLen());
			row.add(vo.getRef());
			row.add(vo.getRows());
			row.add(vo.getExtra());
			rows.add(row);
		});
		return rows;
	}

	@Override
	public String getSheetName() {
		return fileName;
	}

}
